package com.dylan.datacollection.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by dylan on 2016/7/26 0026.
 */
public class SharedPreferencesUtils {
    /*SharedPreferences用户配置文件名称*/
    private static final String CONFIG = "config";
    private static final String GUIDER = "guider";
    public static final String SIGN = "sign";
    public static final String FEATURES_SWITCH = "switch";
    private static final String POPUPACTIVITY = "isShowPopupActivityWindow";
//    public static boolean saveUserInfo(UserEntity userEntity){
//        SharedPreferences sharedPreferences = App.context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
//        SharedPreferences.Editor edit = sharedPreferences.edit();
//        edit.putString("username", userEntity.getUsername());
//        edit.putString("userId", userEntity.getUserId());
//        edit.putString("mobilePhone", userEntity.getMobilePhone());
//        edit.putString("address",userEntity.getAddress());
//        edit.putString("stationId",userEntity.getStationId());
//        edit.putString("stationId",userEntity.getStationId());
//        edit.putString("server",userEntity.getServer());
//        edit.putString("stationName",userEntity.getStationName());
//        edit.putString("telephone",userEntity.getTelephone());
//        edit.putString("goodsList",userEntity.getGoodsList());
//        edit.putInt("stationType",userEntity.getStationType());
//        return edit.commit();
//    }
//    public static UserEntity readUserInfo(){
//        UserEntity userEntity = new UserEntity();
//        SharedPreferences sharedPreferences = App.context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
//        userEntity.setUsername(sharedPreferences.getString("username",""));
//        userEntity.setMobilePhone(sharedPreferences.getString("mobilePhone",""));
//        userEntity.setAddress(sharedPreferences.getString("address",""));
//        userEntity.setStationId(sharedPreferences.getString("stationId",""));
//        userEntity.setServer(sharedPreferences.getString("server",""));
//        userEntity.setStationName(sharedPreferences.getString("stationName",""));
//        userEntity.setTelephone(sharedPreferences.getString("telephone",""));
//        userEntity.setStationType(sharedPreferences.getInt("stationType",-1));
//        userEntity.setUserId(sharedPreferences.getString("userId",""));
//        userEntity.setGoodsList(sharedPreferences.getString("goodsList",""));
//        return userEntity;
//    }

    /**
     * 保存token
     * @param token 用户token
     * @return boolean 是否保存成功
     */
    public static boolean saveToken(Context context,String token) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("token", token);
        return edit.commit();
    }

    /**
     * 读取token
     * @return 用户token，若没有token返回null
     */
    public static String readToken(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        return sharedPreferences.getString("token",null);
    }






    /**保存String类型的数据
     * @param key 标签
     * @param value 值
     * @return
     */
    public static boolean saveString(Context context,String key, String value ){
        SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key,value);
        return edit.commit();
    }

    /**读取String类型的数据
     * @param key 标签
     * @param defaultValue 默认值
     * @return
     */
    public static String readString(Context context,String key, String defaultValue ){
        SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key,defaultValue);
    }

    /**
     * 清空设置
     */
    public static void cleanConfig(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.clear();
        edit.commit();

    }
    public static void saveData(String content) throws IOException {
        File sdCard = Environment.getExternalStorageDirectory();
        File file = new File(sdCard + "/Log", "LocationLog.txt");
        if (!file.exists()) {
            // 获取外部存储设备（SD卡）的路径
            Log.i("===", sdCard.getAbsolutePath());
            // 查看LogCat,获取的sd卡的绝对路径为 /storage/sdcard
            sdCard = new File(sdCard, "/Log");
            sdCard.mkdirs();// 创建MyFiles目录(可创建多级目录)
            file = new File(sdCard, "LocationLog.txt");
        }
        FileOutputStream out = new FileOutputStream(file, true);
        out.write(content.getBytes());
        out.close();
    }






}
